package com.example.system.dao;

import com.example.system.entity.SysBanner;
import com.example.system.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: adnaan
 * create: 2023-11-02 08:23
 * Description:
 */
@Component
public class SysBannerDao {

    @Autowired
    private MongoTemplate mongoTemplate;


    public List<SysBanner> get(Query query) {
        return mongoTemplate.find(query,SysBanner.class);
    }

    public ApiResponse addBanner(SysBanner banner) {
        return ApiResponse.success(mongoTemplate.save(banner));
    }

    public ApiResponse delete(Query query) {
        return ApiResponse.success(mongoTemplate.remove(query,SysBanner.class));
    }

    public ApiResponse update(Query query, Update update) {
        return ApiResponse.success(mongoTemplate.updateMulti(query, update, SysBanner.class));
    }

    public ApiResponse findPaginated(Integer currentPage, Integer pageSize) {
        Query query = new Query();
        long total = mongoTemplate.count(query, SysBanner.class);
        //分页查询
        List<SysBanner> comments = mongoTemplate.find(query.skip((currentPage - 1) * pageSize).limit(pageSize), SysBanner.class);
        //条件体
        Map<String, Object> res = new HashMap<>();
        res.put("total", total);
        res.put("data", comments);

        return ApiResponse.success(res);
    }
}
